package com.lanju.app.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.lanju.app.message.bean.B101Response.ShopBean;
import com.lanju.app.message.bean.B103Request;
import com.lanju.app.message.bean.B103Response;
import com.lanju.app.message.bean.B103Response.ProdStockBean;
import com.lanju.app.message.bean.B103Response.SkuStockBean;
import com.lanju.app.util.AppUtil;
import com.lanju.entity.Shop;
import com.lanju.entity.User;

/**
 * 获取库存信息
 * @author fly
 *
 */
@Service("B103Service")
public class B103Service extends BaseService<B103Request,B103Response>{
	
	@Override
	public B103Response doService(B103Request request, User u) throws Exception {
		
		B103Response res = new B103Response();
		
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT path, id prodId, name, description, ");
		sql.append(" 	fixedNumCur, fixedNum, addNumCur, addNum FROM product a RIGHT JOIN (" );
		sql.append(" 	SELECT prod_id, user_id, SUM(fixed_num_cur) fixedNumCur, SUM(fixed_num) fixedNum, SUM(add_num_cur) addNumCur, ");
		sql.append(" 	SUM(add_num) addNum FROM stock GROUP BY prod_id, user_id) b ON a.id=b.prod_id WHERE user_id=? ");
		List<Object> param = new ArrayList<>();
		param.add(u.getId());
		if(StringUtils.isNotBlank(request.getProdId())){
			sql.append(" AND b.prod_id=? ");
			param.add(request.getProdId());
		}		
		List<ProdStockBean> list = dao.list(sql.toString(), ProdStockBean.class,  param.toArray());
		for(ProdStockBean bean : list){
			bean.setPath(StringUtils.isBlank(bean.getPath())?"":(AppUtil.oss_psth+bean.getPath()));
			bean.setPathSl(StringUtils.isBlank(bean.getPath())?"":(bean.getPath()+AppUtil.oss_bucket_thumb150x150));
		}
		if(StringUtils.isNotBlank(request.getProdId()) && list.size()>0){
			sql = new StringBuilder();
			sql.append(" SELECT DISTINCT b.prod_id prodId, unit, view_unit viewUnit, color,	taste, unit_price unitPrice, a.id stockId, a.prod_sku_id skuId, b.path, ");
			sql.append(" a.add_num addNum,	a.add_num_cur addNumCur, a.fixed_num fixedNum, a.fixed_num_cur fixedNumCur ");
			sql.append(" FROM stock a LEFT JOIN product_sku b ON b.id = a.prod_sku_id WHERE a.prod_id=? AND user_id=? ");
			param = new ArrayList<>();
			param.add(request.getProdId());
			param.add(request.getToken());
			List<SkuStockBean> skuStocks = dao.list(sql.toString(), SkuStockBean.class,  param.toArray());
			for(SkuStockBean bean : skuStocks){
				bean.setPath(StringUtils.isBlank(bean.getPath())?"":(AppUtil.oss_psth+bean.getPath()));
				bean.setPathSl(StringUtils.isBlank(bean.getPath())?"":(bean.getPath()+AppUtil.oss_bucket_thumb150x150));
			}
			list.get(0).setSkuStocks(skuStocks);			
		}
		
		if(StringUtils.isNotBlank(request.getShopId())){
			Shop shop = dao.get(Shop.class, request.getShopId());
			ShopBean bean = new ShopBean();
			if(shop!=null){
				BeanUtils.copyProperties(shop, bean);
				res.setShopBean(bean);
			}
		}
		
		res.setProdStocks(list);
		res.setSuccess(true);
		return res;	
	}
}


